package database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseUtility {

	private static final String DATA_RECORD = "DATA_RECORD";
	private static final String DATABASE_NAME = "FTICO_DATABASE";
	private static final int DATABASE_VERSION = 1;
	private static final String TABLE_USER_PROFILE = "CREATE TABLE USER_PROFILE ("
			+ " USER_ID INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ " USER_NAME TEXT NOT NULL, "
			+ " CREATE_AT TEXT NOT NULL, "
			+ " UPDATE_AT TEXT NOT NULL, " + " DELETE_FLG INTEGER NOT NULL)";
	private static final String TABLE_DEVICE_PROFILE = "CREATE TABLE DEVICE_PROFILE ("
			+ " DEVICE_ID INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ " DEVICE_NAME TEXT NOT NULL, "
			+ " DEVICESERIALNUMBER TEXT NOT NULL, "
			+ " DEVICEMACADDRESS TEXT NOT NULL, "
			+ " DEVICE_TYPE INTEGER NOT NULL, "
			+ " CREATE_AT TEXT NOT NULL, "
			+ " UPDATE_AT TEXT NOT NULL, " + " DELETE_FLG INTEGER NOT NULL)";
	private static final String TABLE_DATA_RECORD = "CREATE TABLE DATA_RECORD ("
			+ " DATA_RECORD_ID INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ " DATA_SERIE INTEGER NOT NULL, "
			+ " TIME_OF_DEVICE TEXT NOT NULL, "
			+ " NUMBER_OF_VALUE INTEGER NOT NULL, "
			+ " V1 INTEGER NOT NULL, "
			+ " V2 INTEGER NOT NULL, "
			+ " V3 INTEGER NOT NULL, "
			+ " V4 INTEGER NOT NULL, "
			+ " V5 INTEGER NOT NULL, "
			+ " V6 INTEGER NOT NULL, "
			+ " V7 INTEGER NOT NULL, "
			+ " V8 INTEGER NOT NULL, "
			+ " V9 INTEGER NOT NULL, "
			+ " V10 INTEGER NOT NULL, "
			+ " CREATE_AT TEXT NOT NULL, "
			+ " UPDATE_AT TEXT NOT NULL, " + " DELETE_FLG INTEGER NOT NULL)";

	private Context mContext;
	private DatabaseHelper mDBHelper;
	private SQLiteDatabase mDb;

	public DatabaseUtility(Context ctx) {
		mContext = ctx;
	}

	public DatabaseUtility open() {
		mDBHelper = new DatabaseHelper(mContext, DATABASE_NAME, null,
				DATABASE_VERSION);
		mDBHelper.set_mContext(mContext);
		mDb = mDBHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		mDBHelper.close();
	}

	public void update() {
		mDBHelper.onUpgrade(mDb, 1, 2);
	}

	public static class DatabaseHelper extends SQLiteOpenHelper {
		private Context mContext;

		public void set_mContext(Context ctx) {
			mContext = ctx;
		}

		public DatabaseHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			db.execSQL(TABLE_USER_PROFILE);
			db.execSQL(TABLE_DEVICE_PROFILE);
			db.execSQL(TABLE_DATA_RECORD);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			db.execSQL("DROP TABLE IF EXIST USER_PROFILE");
			db.execSQL("DROP TABLE IF EXIST DEVICE");
			db.execSQL("DROP TABLE IF EXIST DATA_RECORD");
			onCreate(db);
		}
	}
}
